import cTitle from "components/title";
import { Toast } from 'vant';
import { scrollMixin } from "../../../utils/mixin";
export default {
  mixins: [scrollMixin], //加载更多
  data() {
    return {
      toi: this.fun.getKeyByI(),
      checkList: [],
      datas: [],
      checkAll: false,
      //more
      isLoadMore: true,
      page: 1,
      total_page: 0
    };
  },

  activated() {
    let mailLanguage = JSON.parse(localStorage.getItem("mailLanguage"));
    if (mailLanguage.micro) {
      this.fun.setWXTitle(`${mailLanguage.micro.title}管理`);
    } else {
      this.fun.setWXTitle("微店管理");
    }
    this.toi = this.fun.getKeyByI();
    this.datas = [];
    //获取数据
    this.getData();
  },
  mounted() {
    this.toi = this.fun.getKeyByI();
    let mailLanguage = JSON.parse(localStorage.getItem("mailLanguage"));
    if (mailLanguage.micro) {
      this.fun.setWXTitle(`${mailLanguage.micro.title}管理`);
    } else {
      this.fun.setWXTitle("微店管理");
    }
  },

  methods: {
    // 判断有没有区间价格
    checkPrice(item){
      if(item.vip_level_status && item.vip_level_status.status == 1){
        return item.vip_level_status.word;
      }else if(item.has_option == 1 && item.min_price && this.maxPrice === 'min_price'){
        // 如果有规格，升序并且有区间价格就显示最低价格
        return item.min_price;
      }else if(item.has_option == 1 && item.max_price && this.maxPrice === 'max_price'){
        // 如果有规格，降序并且有区间价格就显示最低价格
        return item.max_price;
      } else if(item.has_option == 1 && item.min_price){
        // 如果有规格，没有按价格排序并且有区间价格就显示最低价格
        return item.min_price;
      }else{
        return item.price;
      }
    },
    //获取数据
    getData() {
      let that = this;
      let json = { i: this.fun.getKeyByI(), type: this.fun.getTyep() };

      $http
        .get(
          "plugin.micro.frontend.controllers.MicroShop.get-goods.index",
          json,
          "加载中"
        )
        .then(
          (response) => {
            console.log(response.data);
            if (response.result == 1) {
              this.isLoadMore = true;
              that.datas = response.data.list.data;
              this.total_page = response.data.list.last_page;
              console.log(response.data.list)
              if (!this.total_page) {
                this.total_page = 0;
              }
            }
          },
          function (response) {
            console.log(response);
          }
        );
    },
    //获取更多数据
    getMoreData() {
      const that = this;
      that.isLoadMore = false; // 防止多次请求分页数据
      let json = { page: 1,i: this.fun.getKeyByI(), type: this.fun.getTyep() };

      if (this.$route.query.plugin_id) json.plugin_id = this.$route.query.plugin_id;//搜索指定插件类型
      if (this.page >= this.total_page) {
        this.isLoadMore = true;
        return;
      } else {
        this.page = this.page + 1;
        json.page = this.page;
        $http.get("plugin.micro.frontend.controllers.MicroShop.get-goods.index", json, "加载中").then(
          response => {
            that.isLoadMore = true;
            if (response.result === 1) {
              let myData = response.data.list.data;
              that.datas = that.datas.concat(myData); //数组拼接
            } else {
              that.page = that.page - 1;
              that.isLoadMore = false;
              return;
            }
          },
          function (response) {
            // error callback
          }
        );
      }
    },
    delteteItem(item, i) {
      console.log(i);
      let that = this;
      let json = {
        id: item.id,
        i: this.fun.getKeyByI(),
        type: this.fun.getTyep()
      };

      $http
        .get(
          "plugin.micro.frontend.controllers.MicroShop.del-goods.index",
          json,
          "处理中..."
        )
        .then(
          function (response) {
            console.log(response);
            if (response.result == 1) {
              that.datas.splice(i, 1);
              Toast("操作成功");
            } else {
              Toast(response.msg);
            }
          },
          function (response) {
            console.log(response);
          }
        );
    },

    toItem(goods) {
      //console.log(goods);
      //this.$router.push({ name: 'goods', params: { id: goods.id } ,query:{i:this.toi}});
      this.$router.push(this.fun.getUrl("goods", { id: goods.id }));
    }

    // handleCheckAllChange(event) {

    //   this.checkList = event.target.checked ? this.datasoruce : [];

    // },
    // handleCheckedChange(event) {
    //   console.log(event);

    //   // let checkedCount = value.length;
    //   // this.checkAll = checkedCount === this.datasoruce.length;

    // },


  },
  components: { cTitle }
};
